﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class InventoryTracker_Invoi : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)

        
    {
        {
            txtDateTime.Text = System.DateTime.Now.ToString();
        }
        
    }

    protected void ddrInvNo_SelectedIndexChanged(object sender, EventArgs e)
    {


        int x = ddrInvNo.SelectedIndex;
        string InvoiceNumber = ddrInvNo.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT [CustName], [CustAddr1], [CustAddr2], [CustAddr3], [CustCity], [CustState], [CustStatus],[CustFax], [CustMobile], [CustPhone], [CustCountry], [CustEmail], [CustZipCode] FROM [CustomerMaster] WHERE ([CustStatus] > 0) AND [CustID]=(SELECT [CustID] FROM [Invoice] WHERE ([InvoiceNumber] =" + InvoiceNumber + "))";
        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        string val = dataTable.Rows[0]["CustName"].ToString() + "<br>" + dataTable.Rows[0]["CustAddr1"].ToString();

   
        if (String.IsNullOrEmpty(dataTable.Rows[0]["CustAddr2"].ToString()))
        {
            val = val + "<br>" + dataTable.Rows[0]["CustAddr2"].ToString();
        }
        val = dataTable.Rows[0]["CustName"].ToString() + "<br>" + dataTable.Rows[0]["CustAddr1"].ToString() + "<br>" + dataTable.Rows[0]["CustAddr2"].ToString();
        if (String.IsNullOrEmpty(dataTable.Rows[0]["CustAddr3"].ToString()))
        {
            val = val + "<br>" + dataTable.Rows[0]["CustAddr3"].ToString();
        }
        val = val + "<br>" + dataTable.Rows[0]["CustAddr3"].ToString() + "<br>" + dataTable.Rows[0]["CustCity"].ToString() + "<br>" +"-"+ dataTable.Rows[0]["CustZipCode"].ToString()+ "<br>" + dataTable.Rows[0]["CustState"].ToString() + "<br>" + dataTable.Rows[0]["CustCountry"].ToString();
        if (String.IsNullOrEmpty(dataTable.Rows[0]["CustFax"].ToString()))
        {
            val = val + "<br>" + dataTable.Rows[0]["CustFax"].ToString();
        }
        if (String.IsNullOrEmpty(dataTable.Rows[0]["CustMobile"].ToString()))
        {
            val = val + "<br>" + dataTable.Rows[0]["CustMobile"].ToString();
        }
        if (String.IsNullOrEmpty(dataTable.Rows[0]["CustEmail"].ToString()))
        {
            val = val + "<br>" + dataTable.Rows[0]["CustEmail"].ToString();
        }
        val = val + "<br>" + dataTable.Rows[0]["CustPhone"].ToString() + "<br>" + dataTable.Rows[0]["CustFax"].ToString() + "<br>" + dataTable.Rows[0]["CustMobile"].ToString() + "<br>" + dataTable.Rows[0]["CustEmail"].ToString() ;
        int r = dataTable.Rows.Count;
        CustLabel.Text = val;

        string val1 = "Name:" + "<br>" + "Address 1:" + "<br>" + " Address 2:" + "<br>" + "Address 3:" + "<br>" + "City:" + "<br>" + "State:" + "<br>" + "Country:" + "<br>" + "Phone No:" + "<br>" + "Mobile No:" + "<br>" + "Fax:" + "<br>" + "E-Mail Id:" + "<br>" + "ZipCode:";
        CustInfLabel.Text = val1;




        String sql2 = "SELECT (InvoiceTotalAmount) FROM [InvoiceDetails] WHERE ([InvoiceNumber] = " + InvoiceNumber + ")";
        //String sql2 = "SELECT SUM(InvoiceItemRate) AS Expr1 FROM [InvoiceDetails] WHERE ([InvoiceNumber] = " + InvoiceNumber + ")";
        SqlCommand cmd2 = new SqlCommand(sql2, con);

        DataTable dataTable2 = GetDataTable(cmd2);
        object Sum = dataTable2.Compute("sum(InvoiceTotalAmount)", "");
        string sumVal = Sum.ToString();

        

        con.Close();
        
        
        

        
     

    }
    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }

    protected void txtDateTime_TextChanged(object sender, EventArgs e)
    {

    }
    protected void DiscountTextBox_TextChanged(object sender, EventArgs e)
    {
        
       
        
    }

    
    protected void TaxTextBox_TextChanged(object sender, EventArgs e)
    {
        
        

    }
    protected void DDRTax_SelectedIndexChanged(object sender, EventArgs e)
    {
        int x = ddrInvNo.SelectedIndex;
        string InvoiceNumber = ddrInvNo.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();

        String sql2 = "SELECT (InvoiceTotalAmount) FROM [InvoiceDetails] WHERE ([InvoiceNumber] = " + InvoiceNumber + ")";
        //String sql2 = "SELECT SUM(InvoiceItemRate) AS Expr1 FROM [InvoiceDetails] WHERE ([InvoiceNumber] = " + InvoiceNumber + ")";
        SqlCommand cmd2 = new SqlCommand(sql2, con);

        DataTable dataTable2 = GetDataTable(cmd2);
        object Sum = dataTable2.Compute("sum(InvoiceTotalAmount)", "");
        string sumVal = Sum.ToString();

        con.Close();
    
       
       if (DiscountTextBox.Text == null || DiscountTextBox.Text == "")
        {
            
           DiscountTextBox.Text = "0";
           
       }

       Double su = Double.Parse(sumVal);
       Double d = Double.Parse(DiscountTextBox.Text);
       su = su - (su * (d / 100));

       GrandTotalTextBox.Text = su.ToString(); 

       Double TA = su;
       Double tax = Double.Parse(DDRTax.SelectedItem.Value);
       Double r = (tax / 100);
       TA = TA + (TA * r);
       TaxTextBox.Text = TA.ToString("N2");
       GrandTotalTextBox.Text = TA.ToString("N2");
      
       
    }

    protected void txtPlace_TextChanged(object sender, EventArgs e)
    {

        
    }
    protected void GrandTotalTextBox_TextChanged(object sender, EventArgs e)
    {
        
        
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
            //string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            //SqlConnection con = new SqlConnection(constr);
            //con.Open();
            //String sql = ("UPDATE InvoiceDetails SET (InvoiceItem = @InvoiceItem, ItemDescription = @ItemDescription, InvoiceItemRate = @InvoiceItemRate, Tax = @Tax,InvoiceTotalAmount = @InvoiceTotalAmount, InvoiceItemQty = @InvoiceItemQty, InvoiceDiscount = @InvoiceDiscount) WHERE (InvoiceNumber =" + InvoiceNumber + ")");
            //SqlCommand cmd = new SqlCommand(sql, con);
            //cmd.ExecuteNonQuery();
            //con.Close();
       

    }
    protected void btnLocSubmit_Click(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = ("UPDATE InvoiceDetails SET (InvoiceItem =  @InvoiceItem, ItemDescription = @ItemDescription, InvoiceItemRate = @InvoiceItemRate, Tax = @Tax,InvoiceTotalAmount = @InvoiceTotalAmount, InvoiceItemQty = @InvoiceItemQty, InvoiceDiscount = @InvoiceDiscount) WHERE (InvoiceNumber =@ InvoiceNumber)");
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();
       
    }
}

